package cn.com.manager.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.com.manager.cache.MyCacheManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.manager.page.PageInfo;
import cn.com.manager.pojo.Demo;
import cn.com.manager.pojo.JsonResult;
import cn.com.manager.service.DemoService;
import cn.com.manager.utility.ApplicationConstants;
import cn.com.manager.utility.SystemUtils;

@Controller
@RequestMapping(value = "/demo")
public class DemoController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(DemoController.class);
	@Autowired
	private DemoService demoService;

	@RequestMapping
	public String index(@ModelAttribute Demo demo, Model model, HttpServletRequest request) {
		if (StringUtils.isNotBlank(demo.getId())) {
			demo = demoService.searchById(demo.getId());
		}
		model.addAttribute("demo", demo);
		return "demo/edit";
	}

	@RequestMapping("/save")
	public String save(@ModelAttribute Demo demo, RedirectAttributes redirectAttributes) {
		int result = demoService.save(demo);
		if (result > 0) {
			redirectAttributes.addFlashAttribute("successMsg", "保存成功！");
		} else {
			redirectAttributes.addFlashAttribute("errorMsg", "保存失败！");
		}
		return "redirect:/demo/search";
	}

	@RequestMapping("/search")
	public String search(@ModelAttribute Demo demo, PageInfo pageInfo, Model model) {
		List<Demo> recordList = demoService.search(demo, pageInfo);
		model.addAttribute("recordList", recordList);
		return "demo/search";
	}

	@RequestMapping("/removeRecord")
	@ResponseBody
	public JsonResult removeRecord(@ModelAttribute Demo demo) {
		int result = demoService.remove(demo);
		JsonResult jsonResult = new JsonResult();
		if (result > 0) {
			jsonResult.setCode(0);
			jsonResult.setMessage("删除成功");
		}
		return jsonResult;
	}

	@RequestMapping("/uploadImg")
	@ResponseBody
	public JsonResult uploadImg(MultipartFile imgFile, HttpServletRequest request) {
		JsonResult jsonResult = new JsonResult();
		try {
			String name = SystemUtils.newUploadFileName(imgFile.getOriginalFilename());
			String uploadPath = servletContext.getRealPath("/upload") + "/" + name;
			imgFile.transferTo(new File(uploadPath));
			jsonResult.setCode(0);
			jsonResult.setMessage("上传成功");
			Map<String,String> params = new HashMap<String,String>();
			params.put("filePath", ApplicationConstants.IMG_WEB + request.getContextPath() + "/upload/" + name);
			jsonResult.setBody(params);
		} catch (Exception e) {
			jsonResult.setCode(0);
			jsonResult.setMessage("上传失败");
			logger.error("文件上传失败",e);
		}
		return jsonResult;
	}

}
